<template>
  <el-header class="header-container">
    <div class="logo-container">
      <!-- <span class="system-title">系统名称</span> -->
    </div>
    <el-menu
      :default-active="activeIndex"
      mode="horizontal"
      class="nav-menu"
      :ellipsis="false"
    >
      <el-menu-item index="1" @click="goToPage('/userIndex/home')">首页</el-menu-item>
      <el-menu-item index="2" @click="goToPage('/userIndex/newsInfo')">新闻中心</el-menu-item>
      <el-menu-item index="3" @click="goToPage('/userIndex/about')">关于我们</el-menu-item>
    </el-menu>
    <div class="user-info">
      <!-- <el-dropdown>
        <span class="el-dropdown-link">
          用户
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>个人信息</el-dropdown-item>
            <el-dropdown-item>修改密码</el-dropdown-item>
            <el-dropdown-item divided>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown> -->
    </div>
  </el-header>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ArrowDown } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const activeIndex = ref('1')

// 根据当前路由设置激活项
const updateActiveIndex = () => {
  const path = route.path
  if (path === '/index') {
    activeIndex.value = '1'
  } else if (path === '/userIndex/newsInfo') {
    activeIndex.value = '2'
  } else if (path === '/userIndex/about') {
    activeIndex.value = '3'
  }
}

// 初始化
updateActiveIndex()

// 监听路由变化
watch(() => route.path, () => {
  updateActiveIndex()
  console.log('路由已变化:', route.path)
})

// 页面跳转方法
const goToPage = (path) => {
  console.log('跳转到:', path)
  console.log('当前路径:', route.path)
  
  if (route.path === path) {
    console.log('已经在当前页面，无需跳转')
    return
  }
  
  router.push(path)
    .then(() => {
      console.log('✅ 跳转成功')
    })
    .catch(err => {
      console.error('❌ 跳转失败:', err)
    })
}
</script>

<style scoped>
.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
  padding: 0 20px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.logo-container {
  display: flex;
  align-items: center;
}

.system-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.nav-menu {
  border-bottom: none;
}

.user-info {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  cursor: pointer;
  color: #333;
  display: flex;
  align-items: center;
}

.el-dropdown-link:hover {
  color: #409eff;
}
</style>